<template>
  <!-- 菜单管理 -->
  <div class="app-container">
    <div class="mainbox">
      <!-- 顶部操作 -->
      <div class="filter-container">
        <el-form ref="filterFormRef" :inline="true" :model="filterForm"
                 class="">
          <el-form-item label="菜单名称" prop="menuName">
            <el-input
              v-model.trim="filterForm.menuName"
              class="filter-item limit"
              maxlength="30"
              style="width: 200px;margin-right:10px"
              clearable
              placeholder="请输入内容"
              @keyup.enter.native="handleFilter"
            />
          </el-form-item>
          <el-form-item label="菜单编号" prop="menuCode">
            <el-input
              v-model.trim="filterForm.menuCode"
              class="filter-item limit"
              maxlength="30"
              style="width: 200px;margin-right:10px"
              clearable
              placeholder="请输入内容"
              @keyup.enter.native="handleFilter"
            />
          </el-form-item>
          <el-form-item label="应用名称" prop="appCode">
            <el-select v-model.trim="filterForm.appCode" placeholder="请选择">
              <el-option
                v-for="item in appList"
                :key="item.appId"
                :label="item.appName"
                :value="item.appCode"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div class="flex-1" />
        <el-button type="primary" @click="handleFilter">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </div>
      <!-- 表格 -->
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <el-button
            v-permission="['AUTH_MENU_ADD_BUTTON']"
            type="primary"
            icon="el-icon-plus"
            @click="onAdd"
          >
            新增
          </el-button>
          <el-button
            v-permission="['AUTH_MENU_OPENALL_BUTTON']"
            icon="el-icon-folder-opened"
            @click="onExpandAll"
          >
            展开所有
          </el-button>
          <el-button
            v-permission="['AUTH_MENU_CLOSEALL_BUTTON']"
            icon="el-icon-folder"
            @click="onFoldAll"
          >
            折叠所有
          </el-button>
          <div class="flex-1" />
          <dt-dialog-column :columns="columns" @queryTable="getList" />
        </div>
        <el-table
          v-if="refreshTable"
          ref="table"
          v-loading="loading"
          border
          :data="tableData"
          style="width: 100%;margin-bottom: 10px;"
          row-key="menuId"
          :default-expand-all="isExpandAll"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :header-cell-style="headerCellStyle"
          :expand-row-keys="expandRowKeys"
        >
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            :index="indexMethod"
          />
          <el-table-column
            v-if="columns[0].show"
            key="menuName"
            prop="menuName"
            label="菜单名称"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[1].show"
            key="menuCode"
            prop="menuCode"
            label="菜单编号"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[2].show"
            key="appName"
            prop="appName"
            label="应用名称"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[3].show"
            key="antdvRouter"
            prop="antdvRouter"
            label="路由地址"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[4].show"
            key="antdvComponent"
            prop="antdvComponent"
            label="组件"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[5].show"
            key="antdvIcon"
            prop="antdvIcon"
            label="图标"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <Item v-if="scope.row.antdvIcon" :icon="scope.row.antdvIcon" />
            </template>
          </el-table-column>
          <!-- <el-table-column
            prop="antdvVisible"
            label="是否可见"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
          >
            <template slot-scope="scope">
              <menu-visiable :visiable="scope.row.antdvVisible" />
            </template>
          </el-table-column> -->
          <el-table-column
            v-if="columns[6].show"
            key="menuSort"
            prop="menuSort"
            label="排序"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[7].show"
            key="statusFlag"
            prop="statusFlag"
            label="是否启用"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.statusFlag"
                :disabled="scope.row.isDefault && scope.row.isDefault === 2"
                prop="statusFlag"
                :active-value="1"
                :inactive-value="2"
                @change="statusFlagChange(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columns[8].show"
            key="menuType"
            prop="menuType"
            label="菜单类型"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <el-tag style="border:none" :type="scope.row.menuType | statusFilter">
                {{ scope.row.menuType == 'M' ? '目录': scope.row.menuType == 'C' ? '菜单':'未知' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            :key="Math.random()"
            :fixed="$store.getters.device == 'mobile'? null :'right' "
            prop="action"
            label="操作"
            width="228"
          >
            <template slot-scope="scope">
              <el-button
                v-permission="['AUTH_MENU_UPDATE_BUTTON']"
                style="padding:7px 10px"
                size="mini"
                :disabled="scope.row.isDefault && scope.row.isDefault === 2"
                @click="onEdit(scope.row)"
              >
                修改
              </el-button>
              <span v-if="scope.row.children.length === 0" style="padding: 10px">
                <el-button
                  slot="reference"
                  v-permission="['AUTH_MENU_BUTTON_MANAGE']"
                  :disabled="scope.row.isDefault && scope.row.isDefault === 2"
                  size="mini"
                  @click="onOpenMenuButton(scope.row)"
                >
                  按钮管理
                </el-button>
              </span>
              <el-button
                slot="reference"
                v-permission="['AUTH_MENU_DEL_BUTTON']"
                :disabled="scope.row.children && scope.row.children.length > 0||scope.row.isDefault && scope.row.isDefault === 2 "
                size="mini"
                type="danger"
                @click="onDelete(scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <MenuModal ref="menuModal" @added="onAdded" @edited="onEdited" />
      <MenuButtonDialog ref="buttonDialog" />
    </div>
  </div>
</template>
<script>
import { getSysMenuList, deleteSysMenu, getLeftMenu, updateStatusSysMenu, getSysAppList } from '@/framework/api/menu'
import { mapGetters } from 'vuex'
import MenuModal from './components/MenuModal'
import MenuButtonDialog from './components/MenuButtonDialog'
import Item from '@/framework/layout/components/Sidebar/Item'
import dialogColumn from '@/framework/utils/mixins/dialogColumn'

export default {
  name: 'AuthMenu',
  components: {
    MenuModal,
    MenuButtonDialog,
    Item
  },
  filters: {
    statusFilter(status) {
      const filter = { M: 'danger', C: 'success' }
      return filter[status]
    }
  },
  mixins: [dialogColumn],
  data() {
    return {
      loading: false,
      refreshTable: true,
      isExpandAll: false,
      filterForm: {
        appCode: '',
        menuCode: '',
        menuName: ''
      },
      // 列信息
      columns: [
        { prop: 'menuName', label: '菜单名称', show: true },
        { prop: 'menuCode', label: '菜单编号', show: true },
        { prop: 'appName', label: '应用名称', show: true },
        { prop: 'antdvRouter', label: '路由地址', show: true },
        { prop: 'antdvComponent', label: '组件', show: true },
        { prop: 'antdvIcon', label: '图标', show: true },
        { prop: 'menuSort', label: '排序', show: true },
        { prop: 'statusFlag', label: '是否启用', show: true },
        { prop: 'menuType', label: '菜单类型', show: true }
      ],
      form: {
        appCode: '',
        antdvIcon: '',
        menuCode: '',
        menuId: '',
        menuName: '',
        menuParentId: '',
        menuParentName: '',
        menuSort: '',
        appName: ''
      },
      tableData: [],
      headerCellStyle: {
        backgroundColor: '#f2f2f2'
      },
      expandRowKeys: [],
      insideDialogVisiable: false,
      appList: []
    }
  },
  computed: {
    ...mapGetters(['roles', 'permission_routes'])
  },
  mounted() {
    this.getList()
    getLeftMenu().then((resp) => {
    })
    this.getAppList()
  },
  methods: {
    // 获取应用列表
    getAppList() {
      getSysAppList().then((res) => {
        if (res.success) {
          this.appList = res.data
        }
      })
    },
    // 列表序号
    indexMethod(index) {
      return index + 1
    },
    // 获取列表
    getList() {
      this.loading = true
      getSysMenuList(this.filterForm).then((resp) => {
        if (resp.success) {
          this.tableData = [...resp.data]
        }
        this.loading = false
      })
    },
    // 搜索
    handleFilter() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.$refs.filterFormRef.resetFields('filterForm')
      this.handleFilter()
    },
    // 点击添加菜单按钮
    onAdd() {
      this.$refs.menuModal.action = 'add'
      this.$refs.menuModal.add()
    },
    onAdded() {
      this.getList()
    },
    // 点击删除按钮
    statusFlagChange(row) {
      const params = {
        menuId: row.menuId,
        statusFlag: row.statusFlag
      }
      updateStatusSysMenu(params).then((resp) => {
        const type = resp.success ? 'success' : 'error'
        // 提示消息
        this.$dtMessage({
          type,
          message: '更新成功'
        })
        if (resp.success) {
          this.getList()
        }
      })
    },
    // 点击删除按钮
    onDelete(row) {
      this.$dtMessageBox({ title: '提示', message: `确定要删除“${row.menuName}”吗?`, type: 'warning' }).then(() => {
        if (row.statusFlag === 1) {
          this.$dtMessage({
            type: 'warning',
            message: '已启用的菜单不能删除'
          })
          return
        }
        const params = {
          menuId: row.menuId
        }
        deleteSysMenu(params).then((resp) => {
          const type = resp.success ? 'success' : 'error'
          // 提示消息
          this.$dtMessage({
            type,
            message: '删除成功'
          })
          if (resp.success) {
            this.getList()
          }
        })
      })
    },
    onExpandAll() {
      // this.tableData.forEach(value => {
      //   if (value.children && value.children.length > 0) {
      //     this.$refs.table.toggleRowExpansion(value, true)
      //   }
      // })
      this.refreshTable = false
      this.isExpandAll = true
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    onFoldAll() {
      // this.tableData.forEach(value => {
      //   this.$refs.table.toggleRowExpansion(value, false)
      // })
      this.refreshTable = false
      this.isExpandAll = false
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    onEdit(row) {
      this.$refs.menuModal.action = 'edit'
      this.$refs.menuModal.edit(row)
    },
    onEdited() {
      this.getList()
    },
    onOpenMenuButton(row) {
      this.$refs.buttonDialog.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.buttonDialog.init(row)
      })
    }
  }
}
</script>

<style lang="scss">
  .menu{
    margin: 20px;
  }
  .tableScopeSwitch .el-switch__label {
    position: absolute;
    display: none;
    padding-left: 4px;
    color: #fff;
  }
  /*打开时文字位置设置*/
  .tableScopeSwitch .el-switch__label--right {
    z-index: 1;
    right: 20px;
  }
  /*关闭时文字位置设置*/
  .tableScopeSwitch .el-switch__label--left {
    z-index: 1;
    left: 18px;
  }
  /*显示文字*/
  .tableScopeSwitch .el-switch__label.is-active {
    display: block;
  }
</style>

<style lang="scss" scoped>
  @import '@/framework/styles/variables';
  .el-dropdown-link {
    cursor: pointer;
    color: #F54A43;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>
